package cloud.take.off.service.impl;

import cloud.take.off.config.RedisUtils;
import cloud.take.off.mapper.ProductInventoryMapper;
import cloud.take.off.module.common.CommonCode;
import cloud.take.off.module.common.ListResult;
import cloud.take.off.module.common.R;
import cloud.take.off.module.order.dto.BaseOrderDTO;
import cloud.take.off.module.order.entity.BaseOrder;
import cloud.take.off.module.order.entity.OrderItem;
import cloud.take.off.module.order.vo.BaseOrderVO;
import cloud.take.off.module.order.vo.OrderItemVO;
import cloud.take.off.module.product.dto.ProductInventoryDTO;
import cloud.take.off.module.product.entity.ProductInventory;
import cloud.take.off.module.product.vo.ProductInventoryVO;
import cloud.take.off.module.system.query.UserInfoDTO;
import cloud.take.off.module.system.vo.UserInfoVO;
import cloud.take.off.mybatis.base.BaseServiceImpl;
import cloud.take.off.mybatis.search.SearchEntity;
import cloud.take.off.mybatis.search.SearchParamEntiy;
import cloud.take.off.mybatis.support.Condition;
import cloud.take.off.mybatis.support.Query;
import cloud.take.off.service.IProductInventoryService;
import cloud.take.off.wrapper.ProductInventoryWrapper;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @Description: TODO
 * @Author: Take-off
 * @Date: 2024/10/14 4:39 PM
 * @Param
 * @return
 * @Version: 1.0
 **/
@Service
@AllArgsConstructor
public class ProductInventoryServiceImpl extends BaseServiceImpl<ProductInventoryMapper, ProductInventory> implements IProductInventoryService {

    private final ProductInventoryWrapper  productInventoryWrapper;

    private final RedisUtils redisUtils;


    @Override
    public boolean submit(ProductInventoryVO productInventoryVO){
        ProductInventory productInventory = productInventoryWrapper.voToEntity(productInventoryVO);
        boolean result = saveOrUpdate(productInventory);
        return result;
    }

    @Override
    public ProductInventoryVO detail(ProductInventoryDTO dto){
        ProductInventoryVO inventoryVO = new ProductInventoryVO();
        ProductInventory inventory = getOne(Condition.getQueryWrapper(productInventoryWrapper.dtoToEntity(dto)));
        if (Optional.ofNullable(inventory).isPresent()){
            inventoryVO = productInventoryWrapper.entityToVO(inventory);
        }
        return inventoryVO;
    }

    @Override
    public ListResult<ProductInventoryVO> list(Query query) {
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs()==null&&query.getDescs()==null){
            query.setDescs("create_time");
        }
        QueryWrapper<ProductInventory> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new ProductInventory()), entityList
        );
        IPage<ProductInventory> pages = page(Condition.getPage(query), queryWrapper);
        List<ProductInventoryVO> productInventoryVOS = productInventoryWrapper.entityToVO(pages.getRecords());
        ListResult<ProductInventoryVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(productInventoryVOS);
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
        return listResult;
    }


}
